package com.kc.sjwh.service;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kc.sjwh.dao.PdtDao;
import com.kc.sjwh.entity.po.Pdt;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;

/**
 * pdt service
 * @author zhangguihua(hua7381@163.com)
 * @date 2017年10月17日
 */
@Service
public class PdtService {
	static Logger logger = Logger.getLogger(PdtService.class);
	@Autowired
	PdtDao pdtDao;

	/**
	 * 分页查询
	 * @param pageSize
	 * @param pageNum
	 * @param param
	 * @return
	 */
	public Page<Pdt> getPage(int pageSize, int pageNum, Map<String, String> param) {
		QueryParam q = new QueryParam(pageSize, pageNum).setOrder("cjsj desc, sbbh");
		
		//添加动态的查询条件
		String v = null;
		v = param.get("keyword");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition("sbbh like ? or sbmc like ?".replace("?", "'%"+v+"%'"));
		}
		v = param.get("dwdm");
		if(StringUtils.isNotEmpty(v)) {
			q.put("zzjgdm", v);
		}
		
		return pdtDao.getPage("get", q);
	}

	/**
	 * 新增
	 * @param one
	 */
	public void add(Pdt one) {
		one.setCjsj(new Date());
		pdtDao.add(one);
	}

	/**
	 * 修改
	 * @param one
	 */
	public void update(Pdt one) {
		one.setGxsj(new Date());
		pdtDao.update(one, "sbbh");
	}

	/**
	 * 删除
	 * @param id
	 */
	@Transactional
	public void delete(String id) {
		//级联删除 none
		
		//删除
		int n = pdtDao.delete(id);
		if(n == 0) {
			throw new IllegalArgumentException("要删除的记录不存在");
		}
	}

	/**
	 * 打开表单
	 * @param id
	 * @return
	 */
	public Pdt form(String id) {
		return id==null ? null : pdtDao.getOne(new QueryParam().put("sbbh", id));
	}
	
}
